package com.cocoblue.nextjslearn;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.cocoblue.nextjslearn.dto.CardData;
import com.cocoblue.nextjslearn.dto.InvoiceDTO;
import com.cocoblue.nextjslearn.entity.Customer;
import com.cocoblue.nextjslearn.entity.Invoice;
import com.cocoblue.nextjslearn.entity.Revenue;
import com.cocoblue.nextjslearn.repo.CustomerRepository;
import com.cocoblue.nextjslearn.repo.InvoiceRepository;
import com.cocoblue.nextjslearn.repo.RevenueRepository;
import com.cocoblue.nextjslearn.repo.SysUserRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Slf4j
@CrossOrigin
@RestController
public class DashboardController {
    @Autowired
    private SysUserRepository sysUserRepository;
    @Autowired
    private InvoiceRepository invoiceRepository;
    @Autowired
    private RevenueRepository revenueRepository;
    @Autowired
    private CustomerRepository customerRepository;

    @GetMapping("/revenue/all")
    public ResponseEntity<List<Revenue>> fetchRevenue(){
        return ResponseEntity.ok(revenueRepository.findAll());
    }

    @GetMapping("/latestInvoice")
    public ResponseEntity<List<InvoiceDTO>> fetchLatestInvoices(){
        List<InvoiceDTO> latestInvoice = invoiceRepository.findLatestInvoice();
        return ResponseEntity.ok(latestInvoice);
    }

    @GetMapping("/cardData")
    public ResponseEntity<CardData> fetchCardData(){

        List<Invoice> invoices = invoiceRepository.findAll();
        List<Customer> customers = customerRepository.findAll();

        return ResponseEntity.ok(new CardData().setNumberOfCustomers(customers.size())
                .setNumberOfInvoices(invoices.size())
                .setTotalPaidInvoices(invoices.stream().filter(e->e.getStatus().equals("paid")).count())
                .setTotalPendingInvoices(invoices.stream().filter(e->e.getStatus().equals("pending")).count())
        );
    }

    @GetMapping("/filteredInvoices")
    public ResponseEntity<List<InvoiceDTO>> fetchFilteredInvoices(@RequestParam("query")String query,
                                                                  @RequestParam("limit")int limit,
                                                                  @RequestParam("offset")int offset){
        List<InvoiceDTO> latestInvoice = invoiceRepository.findInvoiceByFilter(query, limit, offset);
        return ResponseEntity.ok(latestInvoice);
    }

    @GetMapping("/filteredInvoicesPages")
    public ResponseEntity<Integer> filteredInvoicesPages(@RequestParam("query")String query){
        int pages = invoiceRepository.findInvoicePageByFilter(query);
        return ResponseEntity.ok(pages);
    }

    @GetMapping("/customers")
    public ResponseEntity<List<Customer>> fetchCustomers(){
        return ResponseEntity.ok(customerRepository.findAll(Sort.by("name")));
    }

    @GetMapping("/invoice/{id}")
    public ResponseEntity<Invoice> fetchInvoiceById(@PathVariable("id") Long id){
        log.info("Fetch Invoice");

        return ResponseEntity.ok(invoiceRepository.findById(id).orElse(null));
    }


    @GetMapping("/invoice/delete/{id}")
    public ResponseEntity<Invoice> deleteInvoiceById(@PathVariable("id") Long id){
        log.info("Delete Invoice");
        invoiceRepository.deleteById(id);
        return ResponseEntity.ok().build();
    }


    @PostMapping("/invoice/save")
    public ResponseEntity<?> saveInvoice(@RequestBody Invoice invoice){
        if(Objects.nonNull(invoice.getId())){
            Invoice exist = invoiceRepository.findById(invoice.getId()).get();
            BeanUtil.copyProperties(invoice,exist, CopyOptions.create().ignoreNullValue());
            invoice=exist;
        }
        invoiceRepository.save(invoice);
        return ResponseEntity.ok().build();
    }

}
